package com.promote.algorithm;

/**
 * @ClassName Search
 * Description 二分查找---力扣704
 * @Author LiZiHao
 * Date 2021/10/26 15:27
 * @Version 1.0
 **/
public class Search {

    public static int search(int[] nums, int target) {

        int begin = 0;
        int end = nums.length-1;

        while (begin <= end) {

            int mid = (end-begin)/2 + begin;

            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                end = mid -1;
            } else {
                begin = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 12;
        int search = search(nums, target);
        System.out.println(search);
    }
}
